clear;
clc;

cd 'dir'
%%-------------------------------------------------------------------------
%%Part 1: Regressions (Re-weighted)
%%-------------------------------------------------------------------------

%%Plot 1: North American Production Proxy: Treatment
load reg_input.txt

set(0,'DefaultAxesFontSize',15)
na_exp = reg_input(16:29,3)./1000000;
na_exp_se = reg_input(16:29,7)./1000000;
imp_int = reg_input(16:29,4)./1000000;
imp_int_se = reg_input(16:29,8)./1000000;

na_up = na_exp + 1.96*na_exp_se;
na_down = na_exp - 1.96*na_exp_se;
imp_int_up = imp_int + 1.96*imp_int_se;
imp_int_down = imp_int - 1.96*imp_int_se;

time = [-4:1:9]';
zeroline = zeros(length(time),1);

figure(1)
handlevector(1)=plot(time, na_exp,'k','LineWidth',2);
hold on;
handlevector(2)=plot(time,na_up,'k--','LineWidth',1);
handlevector(3)=plot(time,na_down,'k--','LineWidth',1);
handlevector(4)=plot(time,zeroline,'k','LineWidth',1);
grid on;
AX=legend(handlevector([1 2]),...
{'Japanese Firms','95% C.I.'}, 'Location','SouthEast');
LEG = findobj(AX,'type','text');
xlim([-4 9])
set(LEG,'FontSize',15) 
set(gca,'XTick',[-4 -2 0 2 4 6 8 ]);
set(gca,'XTickLabel',[-4 -2 0 2 4 6 8]);
xlabel('Months Relative to Earthquake');
ylabel('Millions of USD')
print(figure(1), '-dpng','-r600','na_exp_fig.png');

figure(2)
handlevector(1)=plot(time, imp_int,'Color',[0 0 0]+.4,'LineStyle','--','LineWidth',2);
hold on;
handlevector(2)=plot(time,imp_int_up,'Color',[0 0 0]+.4,'LineStyle','--','LineWidth',1);
handlevector(3)=plot(time,imp_int_down,'Color',[0 0 0]+.4,'LineStyle','--','LineWidth',1);
handlevector(4)=plot(time,zeroline,'k','LineWidth',1);
grid on;
AX=legend(handlevector([1 2]),...
{'Japanese Firms','95% C.I.'}, 'Location','SouthEast');
LEG = findobj(AX,'type','text');
xlim([-4 9])
set(LEG,'FontSize',15) 
set(gca,'XTick',[-4 -2 0 2 4 6 8 ]);
set(gca,'XTickLabel',[-4 -2 0 2 4 6 8]);
xlabel('Months Relative to Earthquake');
ylabel('Millions of USD')
print(figure(2), '-dpng','-r600','imp_int_fig.png');




na_exp_perc = na_exp(:,1)./3.50489425;
imp_int_perc = imp_int(:,1)./8.0758935;

figure(3)
handlevector(1)=plot(time, imp_int_perc,'Color',[0 0 0]+.4,'LineStyle','--','LineWidth',2);
hold on;
handlevector(2)=plot(time,na_exp_perc,'k','LineWidth',2);
handlevector(3)=plot(time,zeroline,'k','LineWidth',1);
grid on;
AX=legend(handlevector([1 2]),...
{'Imported Inputs','Output (Proxy)'}, 'Location','SouthEast');
LEG = findobj(AX,'type','text');
xlim([-4 9])
set(LEG,'FontSize',15) 
set(gca,'XTick',[-4 -2 0 2 4 6 8 ]);
set(gca,'XTickLabel',[-4 -2 0 2 4 6 8]);
xlabel('Months Relative to Earthquake');
ylabel('Fraction of Pre-Shock Level')
print(figure(3), '-dpng','-r600','percentage_fig_alt.png');
%print(figure(3),'-depsc2' ,'percentage_fig_alt.eps');

%%Plot: Compare Auto Prod Percentage Drop vs Proxy Percentage Drop
clear
load na_prodtype_treatment_nosa_new.txt
x=datenum(2010,11:40,1)';

set(0,'DefaultAxesFontSize',15)
auto_prod = na_prodtype_treatment_nosa_new(15:28,1);

auto_prod_perc = auto_prod./12200;

load reg_input.txt
na_exp = reg_input(16:29,3)./1000000;
na_exp_perc = na_exp(:,1)./3.50489425;


time = [-4:1:9]';
zeroline = zeros(length(time),1);

figure(4)
handlevector(1)=plot(time, na_exp_perc,'k','LineWidth',2);
hold on;
handlevector(2)=plot(time,auto_prod_perc,'b-.','LineWidth',2);
grid on;
AX=legend(handlevector([1 2]),...
{'Output Proxy (NA Exports)','Auto Output'}, 'Location','SouthEast');
LEG = findobj(AX,'type','text');
xlim([-4 9])
set(LEG,'FontSize',15) 
set(gca,'XTick',[-4 -2 0 2 4 6 8 ]);
set(gca,'XTickLabel',[-4 -2 0 2 4 6 8]);
xlabel('Months Relative to Earthquake');
ylabel('Fraction of Pre-Shock Level')
print(figure(4), '-dpng','-r600','prod_perc_compare.png');



%%-------------------------------------------------------------------------
%%Part 2: HP Filtered Data (Unweighted)
%%-------------------------------------------------------------------------

clear;
load reg_input_hp.txt
na_exp = reg_input_hp(6:end,3);
imp_int = reg_input_hp(6:end,4);
na_exp_se = reg_input_hp(6:end,9);
imp_int_se = reg_input_hp(6:end,10);

na_up = na_exp + 1.96*na_exp_se;
na_down = na_exp - 1.96*na_exp_se;
imp_int_up = imp_int + 1.96*imp_int_se;
imp_int_down = imp_int - 1.96*imp_int_se;

time = [-4:1:9]';
zeroline = zeros(length(time),1);

figure(5)
handlevector(1)=plot(time, na_exp,'k','LineWidth',2);
hold on;
handlevector(2)=plot(time,na_up,'k--','LineWidth',1);
handlevector(3)=plot(time,na_down,'k--','LineWidth',1);
handlevector(4)=plot(time,zeroline,'k','LineWidth',1);
grid on;
AX=legend(handlevector([1 2]),...
{'Japanese Firms','95% C.I.'}, 'Location','SouthEast');
LEG = findobj(AX,'type','text');
xlim([-4 9])
set(LEG,'FontSize',15) 
set(gca,'XTick',[-4 -2 0 2 4 6 8 ]);
set(gca,'XTickLabel',[-4 -2 0 2 4 6 8]);
xlabel('Months Relative to Earthquake');
ylabel('Deviations from Trend')
print(figure(5), '-dpng','-r600','na_exp_hp_fig.png');

figure(6)
handlevector(1)=plot(time, imp_int,'b','LineWidth',2);
hold on;
handlevector(2)=plot(time,imp_int_up,'b--','LineWidth',1);
handlevector(3)=plot(time,imp_int_down,'b--','LineWidth',1);
handlevector(4)=plot(time,zeroline,'k','LineWidth',1);
grid on;
AX=legend(handlevector([1 2]),...
{'Japanese Firms','95% C.I.'}, 'Location','SouthEast');
LEG = findobj(AX,'type','text');
xlim([-4 9])
set(LEG,'FontSize',15) 
set(gca,'XTick',[-4 -2 0 2 4 6 8 ]);
set(gca,'XTickLabel',[-4 -2 0 2 4 6 8]);
xlabel('Months Relative to Earthquake');
ylabel('Deviations from Trend')
print(figure(6), '-dpng','-r600','imp_int_hp_fig.png');


figure(7)
handlevector(1)=plot(time, imp_int,'b','LineWidth',2);
hold on;
handlevector(2)=plot(time,na_exp,'k.--','LineWidth',2);
handlevector(3)=plot(time,zeroline,'k','LineWidth',1);
grid on;
AX=legend(handlevector([1 2]),...
{'Imported Inputs','Output (Proxy)'}, 'Location','SouthEast');
LEG = findobj(AX,'type','text');
xlim([-4 9])
set(LEG,'FontSize',15) 
set(gca,'XTick',[-4 -2 0 2 4 6 8 ]);
set(gca,'XTickLabel',[-4 -2 0 2 4 6 8]);
xlabel('Months Relative to Earthquake');
ylabel('Deviations from Trend')
print(figure(7), '-dpng','-r600','percentage_fig_hp_noweight.png');

%%-------------------------------------------------------------------------
%%Part 2: HP Filtered Data (Weighted)
%%-------------------------------------------------------------------------

clear;
load reg_input_hp.txt
na_exp = reg_input_hp(6:end,5);
imp_int = reg_input_hp(6:end,6);
na_exp_se = reg_input_hp(6:end,11);
imp_int_se = reg_input_hp(6:end,12);

na_up = na_exp + 1.96*na_exp_se;
na_down = na_exp - 1.96*na_exp_se;
imp_int_up = imp_int + 1.96*imp_int_se;
imp_int_down = imp_int - 1.96*imp_int_se;

time = [-4:1:9]';
zeroline = zeros(length(time),1);

figure(8)
handlevector(1)=plot(time, na_exp,'k','LineWidth',2);
hold on;
handlevector(2)=plot(time,na_up,'k--','LineWidth',1);
handlevector(3)=plot(time,na_down,'k--','LineWidth',1);
handlevector(4)=plot(time,zeroline,'k','LineWidth',1);
grid on;
AX=legend(handlevector([1 2]),...
{'Japanese Firms','95% C.I.'}, 'Location','SouthEast');
LEG = findobj(AX,'type','text');
xlim([-4 9])
set(LEG,'FontSize',15) 
set(gca,'XTick',[-4 -2 0 2 4 6 8 ]);
set(gca,'XTickLabel',[-4 -2 0 2 4 6 8]);
xlabel('Months Relative to Earthquake');
ylabel('Deviations from Trend')
print(figure(8), '-dpng','-r600','na_exp_hp_w_fig.png');

figure(9)
handlevector(1)=plot(time, imp_int,'b','LineWidth',2);
hold on;
handlevector(2)=plot(time,imp_int_up,'b--','LineWidth',1);
handlevector(3)=plot(time,imp_int_down,'b--','LineWidth',1);
handlevector(4)=plot(time,zeroline,'k','LineWidth',1);
grid on;
AX=legend(handlevector([1 2]),...
{'Japanese Firms','95% C.I.'}, 'Location','SouthEast');
LEG = findobj(AX,'type','text');
xlim([-4 9])
set(LEG,'FontSize',15) 
set(gca,'XTick',[-4 -2 0 2 4 6 8 ]);
set(gca,'XTickLabel',[-4 -2 0 2 4 6 8]);
xlabel('Months Relative to Earthquake');
ylabel('Deviations from Trend')
print(figure(9), '-dpng','-r600','imp_int_hp_w_fig.png');


figure(10)
handlevector(1)=plot(time, imp_int,'b','LineWidth',2);
hold on;
handlevector(2)=plot(time,na_exp,'k.--','LineWidth',2);
handlevector(3)=plot(time,zeroline,'k','LineWidth',1);
grid on;
AX=legend(handlevector([1 2]),...
{'Imported Inputs','Output (Proxy)'}, 'Location','SouthEast');
LEG = findobj(AX,'type','text');
xlim([-4 9])
set(LEG,'FontSize',15) 
set(gca,'XTick',[-4 -2 0 2 4 6 8 ]);
set(gca,'XTickLabel',[-4 -2 0 2 4 6 8]);
xlabel('Months Relative to Earthquake');
ylabel('Deviations from Trend')
print(figure(10), '-dpng','-r600','percentage_fig_hp_weight.png');

%%-------------------------------------------------------------------------
%%Part 3: Distance Figure
%%-------------------------------------------------------------------------

clear;
load dist_data.txt
x=datenum(2009,1:36,1)';

%%Plot 1: Moving Average
set(0,'DefaultAxesFontSize',15)
figure(11)
handlevector(1) = plot(x(2:end-1),dist_data(2:end-1,5),'k','LineWidth',2);
set(gca,'XTick',[733774 733955 734139 734320 734504 734685 734869]);
set(gca,'XTickLabel',[733774 733955 734139 734320 734504 734685 734869]);
ylim([0 0.25])
datetick('x','mmm.yyyy','keepticks')
grid on;
ylabel({'Average Distance from Benchmark Cost Share';'(3 month moving average)'})
print(figure(11), '-dpng','-r600','jpn_dist.png');



clear;
load dist_data.txt
x=datenum(2009,1:36,1)';

%%Plot 12: Moving Average vs non-moving average
set(0,'DefaultAxesFontSize',15)
figure(12)
handlevector(1) = plot(x(2:end-1),dist_data(2:end-1,5),'k','LineWidth',2);
hold on;
handlevector(2) = plot(x(2:end-1),dist_data(2:end-1,3),'b-.','LineWidth',2);
set(gca,'XTick',[733774 733955 734139 734320 734504 734685 734869]);
set(gca,'XTickLabel',[733774 733955 734139 734320 734504 734685 734869]);
AX=legend(handlevector([1 2]),...
{'Moving Average','Monthly'}, 'Location','SouthEast');
LEG = findobj(AX,'type','text');
ylim([0 0.25])
datetick('x','mmm.yyyy','keepticks')
grid on;
ylabel({'Average Distance from Benchmark Cost Share';'(3 month moving average)'})
print(figure(12), '-dpng','-r600','jpn_dist_ma_vs_non.png');
